package multilevelLayoutPlugin;

import static org.junit.Assert.assertTrue;
import giny.model.GraphPerspective;
import giny.model.Node;
import giny.model.RootGraph;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import mTree.MTree;
import mTree.MTreeNode;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import utility.GraphUtility;

public class TestMLLAlgorithm {

    private static final int NODE_COUNT = 5000;
    private static final List<Node> nodes = new ArrayList<Node>();
    private static NodePositionManager posManager;
    private static final int MAXX = 300;
    private static final int MAXY = 300;
    private static final int R = 100;
    private static final Random r = new Random();

    @BeforeClass
    public static void init() {
        posManager = new NodePositionManager();
        for (int i = 0; i < NODE_COUNT; i++) {
            final int id = i;
            nodes.add(new Node() {
                @Override
                public String getIdentifier() {
                    return Integer.toString(id);
                }

                @Override
                public RootGraph getRootGraph() {
                    return null;
                }

                @Override
                public int getRootGraphIndex() {
                    return id;
                }

                @Override
                public boolean setIdentifier(final String arg0) {
                    return false;
                }

                @Override
                public GraphPerspective getGraphPerspective() {
                    return null;
                }

                @Override
                public GraphPerspective getNestedNetwork() {
                    return null;
                }

                @Override
                public boolean setGraphPerspective(final GraphPerspective arg0) {
                    return false;
                }

                @Override
                public void setNestedNetwork(final GraphPerspective arg0) {
                }
            });
            posManager.addNode(id, r.nextInt(MAXX), r.nextInt(MAXY));
        }
    }

    @Test
    public void mTreeVsBruteForceTest() {
        final Node source = nodes.get(r.nextInt(nodes.size()));

        final List<Node> c2 = new ArrayList<Node>();
        final MTree mtree = GraphUtility.constructMTree(nodes.iterator(),
                posManager);
        mtree.getRoot().getRange(new MTreeNode(source), R, c2, 0, posManager);

        final List<Node> c1 = GraphUtility.getCloseNodes(source,
                nodes.iterator(), posManager, R);

        System.out.println(c1.size() + "-" + c2.size());

        for (final Node n : c1) {
            assertTrue(c2.contains(n));
        }
        assertTrue(c1.size() == c2.size());
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

}
